'use strict';
const db = uniCloud.database();
const dbCmd = db.command;
const $ = dbCmd.aggregate;
const {
	mainSpaceId
} = require('config-main-space')
const wmzAdvertiserSdk = uniCloud.importObject('wmz-advertiser-sdk')
exports.main = async (event, context) => {
	
/* let rechargeDatas=	await db.collection('wmzmall-shopie').where({
		profitsharing:true,
		cate:'权益卡购买'
	}).limit(1000).orderBy('create_time', 'desc').get()
	console.log(rechargeDatas) */
	let rechargeDatas = await db.collection('wmz-entitlement-order').where({
		app_id:dbCmd.nin(['0375964d64e56d470223e6a059620ffe','aefa8b0064ec7d3a00056a253341a167'])
	}).limit(1000).orderBy('create_time', 'desc').get()
	rechargeDatas=rechargeDatas.data
	console.log(rechargeDatas.length)
	for (var i in rechargeDatas) {
		await db.collection('wmz-entitlement-order').doc(rechargeDatas[i]._id).update({
			app_id:'aefa8b0064ec7d3a00056a253341a167'
		})
	}
	/* const res = await uniCloud.sendSms({
		appid: '__UNI__EEAF0A3',
		smsKey: "1d987a96a3dc152c55120e6382f6e7f1",
		smsSecret: "9b3ff3f85da19e9a6302d38c6c1266c4",
		phone: '18108851385',
		templateId: '30429', // 请替换为自己申请的模板id
		data: {
			name: '薇云购',
			aname: '代华通讯',
			mobile: '087167275343'
		}
	})
	console.log(res) */

	/* let rechargeDatas = await db.collection('wmz-entitlement-coupon').where({
		status: "未使用",
		ali_user_id: dbCmd.exists(false)
	}).limit(1000).orderBy('create_time', 'desc').get()
	rechargeDatas = rechargeDatas.data */
	/* let monthData = await db.collection('wmz-entitlement-coupon')
		.aggregate()
		.match({
			status: "未使用",
			app_id: dbCmd.exists(false)
		})
		.group({
			_id: '$user_id'
		})
		.skip(1)
		.limit(1000)
		.end()
	monthData = monthData.data
	console.log(monthData.length)
	for (var i in monthData) {
		let entitlementOrder = await db.collection('wmz-entitlement-order').where({
			user_id:monthData[i]._id
		}).get()
		entitlementOrder = entitlementOrder.data[0]
		if (entitlementOrder) {
			console.log(entitlementOrder.ali_user_id)
			await db.collection('wmz-entitlement-coupon').where({
				user_id:monthData[i]._id
			}).update({
				ali_user_id: entitlementOrder.ali_user_id,
				app_id:entitlementOrder.app_id
			})
		}
	} */

	/* let entitlementData = await db.collection('wmz-entitlement').doc("233d13796507db2802bcff9b60361fce").get()
	entitlementData = entitlementData.data[0]
	let callback_count=""
	if(entitlementData){
		callback_count=entitlementData.callback_count+""
	} */
	/* let tCount = 0
	if ('0,2,4,7'.indexOf(tCount) > -1 && tCount < 11) {
		console.log('回传')
	} else {
		if (tCount === 10) {
			
		}
		console.log('不回传') 
	} */

	/* let appId="123"
	let shopId="123"
	let advertisers = await db.collection('wmz-advertiser').where({
		app_id: appId,
		status: 0
	}).get()
	advertisers = advertisers.data
	if (advertisers&&advertisers.length !== 2) {
		let wmzAdvertiserRes = await wmzAdvertiserSdk.callback({
			orderNo:"1410692420025",
			phone: "18788502057",
			price: "30"
		})
		console.log(wmzAdvertiserRes)
		// if (wmzAdvertiserRes.data.code === 200) {
			await db.collection('wmz-advertiser').add({
				orderNo:"1410692420025",
				phone: "18788502057",
				price: "30",
				app_id: appId,
				shop_id: shopId,
				status: 0
			})
		// }
	} else {
		await db.collection('wmz-advertiser').add({
			orderNo:"1410692420025",
			phone: "18788502057",
			price: "30",
			app_id: appId,
			shop_id: shopId,
			status: 2
		})
		await db.collection('wmz-advertiser').where({
			app_id: appId,
			status: 0
		}).update({
			status: 1
		})
	} */

	/* let res=await wmzAdvertiserSdk.callback({
		orderNo:"1410692420025",
		phone: "18788502057",
		price: "30"
	})
	
	console.log(res) */
	/* let rechargeDatas = await db.collection('wmz-entitlement-order').where({
		status: '已付款',
		money:29.9
	}).limit(100).orderBy('create_time', 'desc').get()
	rechargeDatas = rechargeDatas.data
	let tdb = uniCloud.database({
		provider: 'tencent',
		spaceId: mainSpaceId
	})
	let title = '收款';
	let cate = '权益卡购买';
	let type = 0;
	let way ="支付宝";
	for(var i in rechargeDatas){
		let payMoney = rechargeDatas[i].money*100;
		let shopie=await tdb.collection('wmzmall-shopie').where({
			order_no:rechargeDatas[i].trade_no
		}).get()
		shopie=shopie.data[0]
		if(!shopie){
			let shopie = {
				app_id: rechargeDatas[i].app_id,
				shop_id: rechargeDatas[i].shop_id,
				order_no: rechargeDatas[i].trade_no,
				title: title,
				cate: cate,
				type: type,
				way: way,
				money: payMoney, 
				create_time: rechargeDatas[i].create_time
			};
			console.log(shopie)
			let addShopieResult = await db.collection('wmzmall-shopie').add(shopie);
			await tdb.collection('wmzmall-shopie').add(shopie)
		}
	} */
	return event
};